package cn.ytr.crm.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 一简单的生成随机数据的工具类
 * @author YangTianRen
 */
public class RandomUtil {
	//private RandomUtil(){}
	
	//材料库
	private static String lowercase = "qwertyuiopasdfghjklzxcvbnm";
	private static String uppercase = "QWERTYUIOPASDFGHJKLZXCVBNM";
	private static StringBuilder[] emailPostfix = {
			new StringBuilder("@gamil.com"),new StringBuilder("@163.com"),new StringBuilder("@qq.com"),
			new StringBuilder("@126.com"),new StringBuilder("@dingTalk.com"),new StringBuilder("@twin.com")
			};
	private static String[] firstName = {"杨","李","黄","沈","林","张","周","陈","谢"};
	private static String[] lastName = {"天","家","佳","鑫","晓","明","小","亦","红","龙","云","琪","爱","莲",
			"菲","强","宮","桢","金","清","雨","颖","志","平"};
	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
	
	/**
	 * 返回某年一随机 Date
	 * @param year
	 * @return Date
	 */
	public static Date getRandomDate(String year) {
		BigDecimal dateMS = null;
		try {
			dateMS = BigDecimal.valueOf( simpleDateFormat.parse(year).getTime() );
			dateMS = dateMS.add( BigDecimal.valueOf(getRandomInt(0, 31536000-1)).multiply(BigDecimal.valueOf(1000)) );
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date(dateMS.longValue());
	}
	
	/**
	 * 返回一定返回的随机带小数的数
	 * @param min
	 * @param max
	 * @return BigDecimal
	 */
	public static BigDecimal getRandomBigDecimal(int min, int max) {
		BigDecimal num = new BigDecimal(getRandomInt(min, max-1));
		num = num.add( new BigDecimal(getRandomInt(1, 9)).divide(BigDecimal.valueOf(10)) );
		return num;
	}
	
	/**
	 * 返回一随机邮箱
	 * @param nameLength 邮箱名的长度，不包含后缀
	 * @return String
	 */
	public static String getRandomEmail(int nameLength) {
		StringBuilder email = new StringBuilder();
		email.append(uppercase.charAt(getRandomInt(0, uppercase.length()-1)));
		for (int i = 0; i < nameLength-1; i++) {
			email.append(lowercase.charAt(getRandomInt(0, lowercase.length()-1)));
		}
		email.append(emailPostfix[getRandomInt(0, emailPostfix.length-1)]);
		return email.toString();
	}
	
	/**
	 * 返回某一范围内的随机整数
	 * @param start
	 * @param end
	 * @return int
	 */
	public static int getRandomInt(int min, int max) {
		//new Random().nextInt(30) 就是 [0,30)的随机数，若要[20,30],拆分为 [20, 30+20-20]
		return new Random().nextInt(max-min+1)+min;
	}
	
	
	/**
	 * 返回一个随机名字
	 * @return String
	 */
	public static String getRandomName() {
		String name = firstName[getRandomInt(0, firstName.length-1)] +
				lastName[getRandomInt(0, lastName.length-1)] +
				lastName[getRandomInt(0, lastName.length-1)];
		return name;
	}
	
}
